package com.bitdf.txing.seckill.config;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;


@Configuration
@ConditionalOnProperty(value = "tx.deploy.mode", havingValue = "cloud")
public class MyRedissonConfig {

    /**
     * 所有对Redisson的使用都是通过RedissonClient
     * @return
     * @throws IOException
     */
    @Bean(destroyMethod="shutdown")
    public RedissonClient redissonClient() throws IOException {
        //1、创建配置
        Config config = new Config();
        config.useSingleServer().setAddress("redis://124.71.1.148:6379").setPassword("lzw_redis").setDatabase(1)
//                .setTimeout(1000)
//                .setRetryAttempts(3)
//                .setRetryInterval(1000)
                .setPingConnectionInterval(1000) //**此项务必设置为redisson解决之前bug的timeout问题关键*****
//                .setDatabase(3)
        ;

        //2、根据Config创建出RedissonClient实例
        //Redis url should start with redis:// or rediss://
        RedissonClient redissonClient = Redisson.create(config);
        return redissonClient;
    }

}
